const joi = require('joi');
const dbQuery = require('../model/db');
const {
	sendError,
	sendSuccess
} = require('../utils/sendFunction');

// 验证规则
const schemaCollect = joi.object({
	userid: joi.number().required().error(new Error('userid不符合验证规则')),
	type: joi.number().valid(0, 1, 2).required().error(new Error('type不符合验证规则'))
})

const schemaCollect0 = joi.object({
	musicid: joi.number().required().error(new Error('musicid不符合验证规则'))
})

const schemaCollect1 = joi.object({
	gedanid: joi.number().required().error(new Error('gedanid不符合验证规则'))
})

const schemaCollect2 = joi.object({
	mvid: joi.number().required().error(new Error('mvid不符合验证规则'))
})

// userid type(0music 1gedan 2mv) mvid gedanid musicid
const addCollect = async function (req, res, next) {
	let info = req.body;

	if (!info.userid || !String(info.type)) {
		sendError(res, 200, 'userid或type不能为空!');
		return;
	}

	try {
		await schemaCollect.validateAsync({
			userid: info.userid,
			type: info.type
		})
	} catch (error) {
		next(error);
		return;
	}

	// 歌曲
	if (info.type == 0) {
		try {
			await schemaCollect0.validateAsync({
				musicid: info.musicid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'select * from collect where user_id=? and music_id=?';
		let result = await dbQuery(sqlStr, [info.userid, info.musicid]);
		if (result.length !== 0) {
			sendError(res, 200, '添加收藏失败，已收藏了!')
			return
		}

		let sqlStr1 = 'insert into collect (user_id,type,music_id) values (?,?,?)';
		let result1 = await dbQuery(sqlStr1, [info.userid, info.type, info.musicid]);
		if (result1.affectedRows == 1) {
			sendSuccess(res, '添加收藏成功!');
		} else {
			sendError(res, 500, '添加收藏失败，请稍后再试!')
		}
		// 歌单
	} else if (info.type == 1) {
		try {
			await schemaCollect1.validateAsync({
				gedanid: info.gedanid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'select * from collect where user_id=? and gedan_id=?';
		let result = await dbQuery(sqlStr, [info.userid, info.gedanid]);
		if (result.length !== 0) {
			sendError(res, 200, '添加收藏失败，已收藏了!')
			return
		}

		let sqlStr1 = 'insert into collect (user_id,type,gedan_id) values (?,?,?)';
		let result1 = await dbQuery(sqlStr1, [info.userid, info.type, info.gedanid]);
		if (result1.affectedRows == 1) {
			sendSuccess(res, '添加收藏成功!');
		} else {
			sendError(res, 500, '添加收藏失败，请稍后再试!')
		}
		// mv
	} else {
		try {
			await schemaCollect2.validateAsync({
				mvid: info.mvid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'select * from collect where user_id=? and mv_id=?';
		let result = await dbQuery(sqlStr, [info.userid, info.mvid]);
		if (result.length !== 0) {
			sendError(res, 200, '添加收藏失败，已收藏了!')
			return
		}

		let sqlStr1 = 'insert into collect (user_id,type,mv_id) values (?,?,?)';
		let result1 = await dbQuery(sqlStr1, [info.userid, info.type, info.mvid]);
		if (result1.affectedRows == 1) {
			sendSuccess(res, '添加收藏成功!');
		} else {
			sendError(res, 500, '添加收藏失败，请稍后再试!')
		}
	}
}

// userid type(0music 1gedan 2mv) mvid gedanid musicid
const delectCollect = async function (req, res, next) {
	let info = req.body;

	if (!info.userid || !String(info.type)) {
		sendError(res, 200, 'userid或type不能为空!');
		return;
	}

	try {
		await schemaCollect.validateAsync({
			userid: info.userid,
			type: info.type
		})
	} catch (error) {
		next(error);
		return;
	}

	// 歌曲
	if (info.type == 0) {
		try {
			await schemaCollect0.validateAsync({
				musicid: info.musicid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'select * from collect where user_id=? and music_id=?';
		let result = await dbQuery(sqlStr, [info.userid, info.musicid]);
		if (result.length == 0) {
			sendError(res, 200, '取消收藏失败，已经取消收藏了!')
			return
		}

		let sqlStr1 = 'delete from collect where user_id=? and type=? and music_id=?';
		let result1 = await dbQuery(sqlStr1, [info.userid, info.type, info.musicid]);
		if (result1.affectedRows == 1) {
			sendSuccess(res, '取消收藏成功!');
		} else {
			sendError(res, 500, '取消收藏失败，请稍后再试!')
		}
		// 歌单
	} else if (info.type == 1) {
		try {
			await schemaCollect1.validateAsync({
				gedanid: info.gedanid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'select * from collect where user_id=? and gedan_id=?';
		let result = await dbQuery(sqlStr, [info.userid, info.gedanid]);
		if (result.length == 0) {
			sendError(res, 200, '取消收藏失败，已经取消收藏了!')
			return
		}

		let sqlStr1 = 'delete from collect where user_id=? and type=? and gedan_id=?';
		let result1 = await dbQuery(sqlStr1, [info.userid, info.type, info.gedanid]);
		if (result1.affectedRows == 1) {
			sendSuccess(res, '取消收藏成功!');
		} else {
			sendError(res, 500, '取消收藏失败，请稍后再试!')
		}
		// mv
	} else {
		try {
			await schemaCollect2.validateAsync({
				mvid: info.mvid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'select * from collect where user_id=? and mv_id=?';
		let result = await dbQuery(sqlStr, [info.userid, info.mvid]);
		if (result.length == 0) {
			sendError(res, 200, '取消收藏失败，已经取消收藏了!')
			return
		}

		let sqlStr1 = 'delete from collect where user_id=? and type=? and mv_id=?';
		let result1 = await dbQuery(sqlStr1, [info.userid, info.type, info.mvid]);
		if (result1.affectedRows == 1) {
			sendSuccess(res, '取消收藏成功!');
		} else {
			sendError(res, 500, '取消收藏失败，请稍后再试!')
		}
	}
}

// userid type(0music 1gedan 2mv)
const getCollect = async function (req, res, next) {
	let info = req.query;

	if (!info.userid || !String(info.type)) {
		sendError(res, 200, 'userid或type不能为空!');
		return;
	}

	try {
		await schemaCollect.validateAsync({
			userid: info.userid,
			type: info.type
		})
	} catch (error) {
		next(error);
		return;
	}

	// 歌曲
	if (info.type == 0) {
		let sqlStr = 'select collect.*,music.*,singer.* from collect inner join music on collect.music_id = music.musicid INNER JOIN singer on music.singer_id = singer.singerid where collect.user_id=? and collect.type=? order by collect.collectid desc';
		let result = await dbQuery(sqlStr, [info.userid, info.type]);
		if (result.length >= 0) {
			sendSuccess(res, '获取收藏成功!', {
				data: result
			})
		} else {
			sendError(res, 500, '获取收藏失败，请稍后再试!')
		}
		// 歌单
	} else if (info.type == 1) {
		let sqlStr = 'select collect.*,gedan.* from collect inner join gedan on collect.gedan_id = gedan.gedanid where collect.user_id=? and collect.type=? order by collect.collectid desc';
		let result = await dbQuery(sqlStr, [info.userid, info.type]);
		if (result.length >= 0) {
			sendSuccess(res, '获取收藏成功!', {
				data: result
			})
		} else {
			sendError(res, 500, '获取收藏失败，请稍后再试!')
		}
		// mv
	} else {
		let sqlStr = 'select collect.*,mv.*,singer.* from collect inner join mv on collect.mv_id = mv.mvid INNER JOIN singer on mv.singer_id = singer.singerid where collect.user_id=? and collect.type=? order by collect.collectid desc';
		let result = await dbQuery(sqlStr, [info.userid, info.type]);
		if (result.length >= 0) {
			sendSuccess(res, '获取收藏成功!', {
				data: result
			})
		} else {
			sendError(res, 500, '获取收藏失败，请稍后再试!')
		}
	}
}

// userid type(0music 1gedan 2mv) mvid gedanid
const pdCollect = async function (req, res, next) {
	let info = req.query;

	if (!info.userid || !info.type) {
		sendError(res, 200, 'userid或type不能为空!');
		return;
	}

	try {
		await schemaCollect.validateAsync({
			userid: info.userid,
			type: info.type
		})
	} catch (error) {
		next(error);
		return;
	}

	if (info.type == 1) {
		try {
			await schemaCollect1.validateAsync({
				gedanid: info.gedanid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'select collect.* from collect where collect.user_id=? and collect.type=? and collect.gedan_id=?';
		let result = await dbQuery(sqlStr, [info.userid, info.type, info.gedanid]);
		if (result.length !== 0) {
			sendSuccess(res, '已收藏！')
		} else {
			sendError(res, 200, '未收藏！')
		}
		// mv
	} else {
		try {
			await schemaCollect2.validateAsync({
				mvid: info.mvid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'select collect.* from collect  where collect.user_id=? and collect.type=? and collect.mv_id=?';
		let result = await dbQuery(sqlStr, [info.userid, info.type, info.mvid]);
		if (result.length !== 0) {
			sendSuccess(res, '已收藏！')
		} else {
			sendError(res, 200, '未收藏！')
		}
	}
}

module.exports = {
	addCollect,
	delectCollect,
	getCollect,
	pdCollect
}